import { buildUUID } from '@/utils/uuid.js'

/**
 * backsave 前端选择后 设置true,后端只做存储
 * backset true 表示为后端初始化，前端从未提交
 */
export function useActionPlan(type) {
  // 哮喘固定值
  const asthmaType = 4
  // 哮喘分类字典
  const asthmaDicToArray = [
    'actionYellow',
    'usageYellow',
    'usageRed',
    'actionRed',
    'usageGreen',
    'emergencyUse',
    'actionGreen',
    'cnUse',
  ]
  // 预留type用以区分不同的病种
  const docType = type
  function initActionPlanData(data) {
    if (data) {
      const { controLevel, dto, acuteSeverity, tcm } = data
      const {
        status,
        actionPlanType,
        content,
        treatmentPlan,
        addTreatmentPlan,
        desensitizationTreatment,
      } = dto || {}
      return {
        recommendControLevel: controLevel ? controLevel : null,
        recommendAcuteSeverity: acuteSeverity ? acuteSeverity : null,
        sendDisabled: status === 2 ? true : false,
        disabled: status === 2 ? true : false,
        actionPlanType,
        content,
        treatmentPlan,
        addTreatmentPlan,
        desensitizationTreatment,
        isCn: tcm === 1 ? true : false,
      }
    }
  }
  /**
   * 模版数据更新 所有数据需要走这里，分配uuid。
   * @param {*} values
   * @param {*} actionPlanValue
   */
  function templateUpdateData(values, actionPlanValue) {
    console.log('templateUpdateData =' + JSON.stringify(values))
    const { selData = {}, templateType } = values
    let updateData = {}

    asthmaDicToArray.map((item) => {
      if (selData && Array.isArray(selData[item]) && selData[item].length > 0) {
        if (item === 'emergencyUse' && templateType === 3) {
          // templateType 为3
          updateData['cnUse'] = selData[item]
          // d['cnUse'] = d['cnUse'].concat(selData[item])
        } else {
          updateData[item] = selData[item]
          // d[item] = d[item].concat(selData[item])
        }
      }
    })
    return updateData
  }

  /**
   * 外部进入初始化数据，给药物对象分配uuid
   */
  function initialize(data) {
    let updateData = {}
    // 适配模板调用页面
    asthmaDicToArray.map((item) => {
      updateData[item] = data[item]
        ? data[item].map((element) => {
            element.uuid = buildUUID()
            return element
          })
        : []
    })
  }
  function cloneAction(self, actionData) {
    if (!actionData) return self
    Object.keys(actionData).forEach((item) => {
      if (actionData[item]) {
        self[item] = actionData[item]
      }
    })
  }

  function filterUpdate(data, updateData, index) {
    // todo 改成替换会不会好一点
    if (data && data.length > 0) {
      if (index !== null) {
        const tmp = data.map((element, key) => {
          if (key === index) {
            return { ...element, ...updateData }
          }
          return element
        })
        return tmp
      }
      data.push(updateData)
      return data
    }
    data.push(updateData)
    return data
  }

  /**
   *
   * @param {*} initial 预设置
   * @param {*} modify 后面自己提交的修改值
   */
  function diffUsageGreen(initial, modify) {
    if (modify && modify.length > 0) {
      return initial.map((item) => {
        let tmp = item
        modify.forEach((element) => {
          if (item.drugId === element.drugId) {
            element.backSet = true
            element.backSave = true
            tmp = element
          }
        })
        return tmp
      })
    } else {
      return initial
    }
  }
  return {
    initActionPlanData,
    templateUpdateData,
    initialize,
    cloneAction,
    filterUpdate,
    diffUsageGreen,
  }
}
